package org.jeecg.modules.qyxx.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.qyxx.entity.BgFpHzNdXyjxs;
import org.jeecg.modules.qyxx.entity.resp.BgFpHzNdXyjxsVo;
import org.jeecg.modules.qyxx.mapper.BgFpHzNdXyjxsMapper;
import org.jeecg.modules.qyxx.service.IBgFpHzNdXyjxsService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 下游经销商汇总数据
 * @Author: ZJJ
 * @Date: 2021-06-09
 * @Version: V1.0
 */
// @DS("multi-datasource1")申报
@Service
public class BgFpHzNdXyjxsServiceImpl extends MppServiceImpl<BgFpHzNdXyjxsMapper, BgFpHzNdXyjxs> implements IBgFpHzNdXyjxsService {
    @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;

    @Override
    public List xyjxssjhz(String djxh, List<String> tjnds,Date jssj) {
        DateUtils utils = DateUtils.getInstance();
        List<BgFpHzNdXyjxsVo> xyjxssjhz = baseMapper.xyjxssjhz(djxh, String.join(",", tjnds));
        // String year = bgSjmkjgOrderService.getNewByDjxh(djxh);
        String year = utils.getYearToStr(jssj);
        for (BgFpHzNdXyjxsVo bgFpHzNdXyjxsVo : xyjxssjhz) {
            if (bgFpHzNdXyjxsVo.getJxsFpje() != null) {
                bgFpHzNdXyjxsVo.setJxsFpje(bgFpHzNdXyjxsVo.getJxsFpje().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            if (bgFpHzNdXyjxsVo.getJxsFpje() != null) {
                bgFpHzNdXyjxsVo.setJxsFpse(bgFpHzNdXyjxsVo.getJxsFpse().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
            }
            bgFpHzNdXyjxsVo.setTsYear(bgFpHzNdXyjxsVo.getTjnd() + "年");
            if (year.equals(bgFpHzNdXyjxsVo.getTjnd())) {
                // String month = bgSjmkjgOrderService.getNewMonthByDjxh(djxh);
                String month = utils.getMonthToStr(jssj);
                String newMonth = month.substring(month.length() - 2);
                bgFpHzNdXyjxsVo.setTjnd(year + "年(" + newMonth + "月)");
            }else {
                bgFpHzNdXyjxsVo.setTjnd(bgFpHzNdXyjxsVo.getTjnd() + "年");
            }
        }
        return xyjxssjhz;
    }

    @Override
    public BgFpHzNdXyjxs getByDjxhAndTjnd(String djxh, String tjnd) {
        LambdaQueryWrapper<BgFpHzNdXyjxs> wrapper = Wrappers.lambdaQuery(BgFpHzNdXyjxs.class);
        wrapper.eq(BgFpHzNdXyjxs::getDjxh,djxh).eq(BgFpHzNdXyjxs::getTjnd,tjnd);
        return getOne(wrapper);
    }
}
